//
//  BaseMoreVideoVC.m
//  Project2
//
//  Created by hundredsandc on 2017/11/8.
//  Copyright © 2017年 hundredsandc. All rights reserved.
//

#import "BaseMoreVideoVC.h"
#import "JiJinModel.h"
#import "HomeCollectionViewCell.h"


@interface BaseMoreVideoVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong) UICollectionView  *collectionView;
@property (nonatomic,strong)UICollectionViewFlowLayout *flowLayOut;
@property (nonatomic,strong)NSMutableArray *videoArr;
@end

@implementation BaseMoreVideoVC
{
    NSInteger type;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self getTypeData];
    [self setupUI];
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}
-(void)setupUI{
    [self.view addSubview:self.collectionView];
    [self addConstraint];
}
#pragma mark - constraint
-(void)addConstraint{
    WS(ws);
    
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ws.view);
        make.bottom.mas_equalTo(ws.view).with.offset(-64);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.left.equalTo(ws.view);
    }];
    
}
-(void)getTypeData{
    
    //    http://cdn.a8.tvesou.com/right/hot_video_list?
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    //    0 全部 1nba 2cba 3德甲 4英超 5意甲 6西甲 7法甲 8欧冠 9中超
    //    http://cdn.a8.tvesou.com/right/recommend_video_list?leagueId=4&page=0&pageSize=20
    
    params[@"leagueId"] = @(self.index);
    params[@"page"] = @(0);
    params[@"pageSize"] = @(20);
    [HTTPManagers getWithLiveURL:@"recommend_video_list" params:params success:^(id json) {
        NSArray *arr= [json objectForKey:@"data"];
        if (self.videoArr.count>0) {
            [_videoArr removeAllObjects];
        }
        for (int i = 0; i<arr.count; i++) {
            NSDictionary *dict = arr[i];
            JiJinModel *model = [JiJinModel mj_objectWithKeyValues:dict];
            [_videoArr addObject:model];
            
        }
        [_collectionView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return _videoArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
   
        HomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([HomeCollectionViewCell class]) forIndexPath:indexPath];
        
        if (_videoArr.count>0) {
            
            JiJinModel *model = self.videoArr[indexPath.row];
            NSString *imageUrlStr = [[model.videos firstObject]objectForKey:@"thumb"];
            
            
            
            
            [cell customImageUrl:imageUrlStr time:model.source title:model.title type:nil];
        }
        return cell;
   
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

    return UIEdgeInsetsMake(10, 15, 0, 15);
    
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((SCREEN_WIDTH-39)/2, 137+10);
   
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    if (_videoArr.count>0) {
        
    
        JiJinModel *model = self.videoArr[indexPath.row];
        NSString *url = [[model.videos firstObject]objectForKey:@"url"];
        WEBViewController *web = [[WEBViewController alloc]init];
        web.titleStr = @"视屏";
        web.url = url;
        [self.navigationController pushViewController:web animated:YES];
    }
    
}

-(UICollectionView *)collectionView{
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.flowLayOut];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];

        [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([HomeCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([HomeCollectionViewCell class])];
    }
    return _collectionView;
}
-(UICollectionViewFlowLayout *)flowLayOut{
    if (!_flowLayOut) {
        _flowLayOut = [[UICollectionViewFlowLayout alloc] init];
        
        _flowLayOut.minimumInteritemSpacing = 9;
        _flowLayOut.minimumLineSpacing = 15;
        [_flowLayOut setScrollDirection:UICollectionViewScrollDirectionVertical];
        
    }
    return _flowLayOut;
}
-(NSMutableArray *)videoArr{
    if (!_videoArr) {
        _videoArr = [NSMutableArray array];
    }
    return _videoArr;
}
@end
